package top.xierongsheng.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.xierongsheng.user.dao.ResourceMapper;
import top.xierongsheng.user.dto.ResourceDto;
import top.xierongsheng.user.model.SysResource;
import top.xierongsheng.user.service.IResourceService;
import top.xierongsheng.utlis.data.RandomUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 资源表 服务实现类
 *
 * @auther 谢荣生
 * @datetime 2020/10/16 17:23
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, SysResource> implements IResourceService {

    @Autowired(required = false)
    private ResourceMapper resourceMapper;

    /**
     * 新增资源
     *
     * @param resourceDto
     * @param userId
     * @return
     */
    @Override
    public int addResource(ResourceDto resourceDto, String userId) {
        SysResource resource = new SysResource();
        resource.setId(RandomUtil.getUUID());
        resource.setParentId(resourceDto.getParentId());
        resource.setName(resourceDto.getName());
        resource.setUrl(resourceDto.getUrl());
        resource.setType(resourceDto.getType());
        resource.setLevel(resourceDto.getLevel());
        resource.setPriority(resourceDto.getPriority());
        resource.setIcon(resourceDto.getIcon());
        resource.setPermission(resourceDto.getPermission());
        resource.setStatus(1);
        resource.setCreateTime(new Date());
        resource.setUpdateTime(new Date());
        resource.setUpdateId(userId);
        resource.setCreatorId(userId);
        return resourceMapper.insert(resource);
    }

    /**
     * 编辑资源
     *
     * @param resourceDto
     * @param userId
     * @return
     */
    @Override
    public int editResource(ResourceDto resourceDto, String userId) {
        SysResource resource = resourceMapper.selectById(resourceDto.getId());
        resource.setParentId(resourceDto.getParentId());
        resource.setName(resourceDto.getName());
        resource.setUrl(resourceDto.getUrl());
        resource.setType(resourceDto.getType());
        resource.setLevel(resourceDto.getLevel());
        resource.setPriority(resourceDto.getPriority());
        resource.setIcon(resourceDto.getIcon());
        resource.setPermission(resourceDto.getPermission());
        resource.setStatus(1);
        resource.setUpdateTime(new Date());
        resource.setUpdateId(userId);
        return resourceMapper.updateById(resource);
    }

    /**
     * 根据菜单名及菜单类型进行模糊分页查询
     *
     * @param page 分页
     * @param name 菜单名称
     * @param type 菜单类型
     * @return
     * @auther 谢荣生
     * @datetime 2020/11/3 14:50
     */
    @Override
    public IPage<HashMap> getResourceListPage(Page<?> page, String name, Integer type) {
        return resourceMapper.getResourceListPage(page, name, type);
    }

    /**
     * 根据菜单名及菜单类型进行模糊查询获取所有数据
     *
     * @param name
     * @param type
     * @return
     */
    @Override
    public List<HashMap> getResourceList(String name, Integer type) {
        return resourceMapper.getResourceList(name, type);
    }
}
